package com.framework.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.core.log.annotation.AutoLog;
import com.framework.core.result.R;
import com.framework.system.entity.dto.SysConfigAddDTO;
import com.framework.system.entity.dto.SysConfigQueryDTO;
import com.framework.system.entity.dto.SysConfigUpdateDTO;
import com.framework.system.entity.vo.SysConfigListVO;
import com.framework.system.entity.vo.SysConfigVO;
import com.framework.system.service.ISysConfigService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**
 * 系统配置管理
 *
 * @author Kevin
 * @since 2021-06-10
 */
@RestController
@RequestMapping("/system/sysConfig")
@AllArgsConstructor
public class SysConfigController {

    private ISysConfigService sysConfigService;

    /**
     * 分页查询系统配置列表
     */
    @GetMapping("/selectSysConfigWithPage")
    @SaCheckPermission("system:sysConfig:selectSysConfigWithPage")
    @AutoLog(module = "参数管理", title = "分页查询系统配置")
    public R<Page<SysConfigListVO>> selectSysConfigWithPage(Page reqPage, SysConfigQueryDTO req) {
        return R.success(sysConfigService.selectSysConfigWithPage(reqPage, req));
    }

    /**
     * 新增系统配置
     */
    @PostMapping("/addSysConfig")
    @SaCheckPermission("system:sysConfig:addSysConfig")
    @AutoLog(module = "参数管理", title = "新增系统配置")
    public R<Void> addSysConfig(@Validated @RequestBody SysConfigAddDTO req) {
        sysConfigService.addSysConfig(req);
        return R.success();
    }

    /**
     * 修改系统配置
     */
    @PostMapping("/updateSysConfig")
    @SaCheckPermission("system:sysConfig:updateSysConfig")
    @AutoLog(module = "参数管理", title = "修改系统配置")
    public R<Void> updateSysConfig(@Validated @RequestBody SysConfigUpdateDTO req) {
        sysConfigService.updateSysConfig(req);
        return R.success();
    }

    /**
     * 删除系统配置
     */
    @PostMapping("/removeSysConfig")
    @SaCheckPermission("system:sysConfig:removeSysConfig")
    @AutoLog(module = "参数管理", title = "删除系统配置")
    public R<Void> removeSysConfig(@RequestParam String ids) {
        sysConfigService.removeSysConfig(ids);
        return R.success();
    }

    /**
     * 查看系统配置
     */
    @GetMapping("/getSysConfig")
    @SaCheckPermission("system:sysConfig:getSysConfig")
    @AutoLog(module = "参数管理", title = "查看系统配置")
    public R<SysConfigVO> getSysConfig(@RequestParam String id) {
        return R.success(sysConfigService.getSysConfig(id));
    }

    /**
     * 导出系统配置
     */
    @GetMapping("/exportSysConfig")
    @SaCheckPermission("system:sysConfig:exportSysConfig")
    @AutoLog(module = "参数管理", title = "导出系统配置")
    @SneakyThrows
    public void exportSysConfig(SysConfigQueryDTO req, HttpServletResponse response) {
        String filename = "参数列表" + "_" + System.currentTimeMillis() + ".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        EasyExcel.write(response.getOutputStream())
                .head(SysConfigListVO.class)
                .sheet("参数列表")
                .doWrite(sysConfigService.selectSysConfigWithList(req));
    }

    /**
     * 根据参数键名获取参数键值
     */
    @GetMapping("/getConfigValue")
    @AutoLog(module = "参数管理", title = "根据参数键名获取参数键值")
    public R<String> getConfigValue(@RequestParam String key) {
        return R.success(sysConfigService.getConfigValueByKey(key));
    }

    /**
     * 刷新系统配置缓存
     */
    @PostMapping("/refreshSysConfigCache")
    @SaCheckPermission("system:sysConfig:refreshSysConfigCache")
    @AutoLog(module = "参数管理", title = "刷新系统配置缓存")
    public R<Void> refreshSysConfigCache() {
        sysConfigService.clearSysConfigAllCache();
        return R.success();
    }

}

